<title>后台管理员</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>用户</cite></a>
    <a><cite>后台管理员</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
      <div class="layui-form-item">
        <!-- 用户名搜索 -->
        <div class="layui-inline">
          <label class="layui-form-label">用户名</label>
          <div class="layui-input-block">
            <input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">真实姓名</label>
          <div class="layui-input-block">
            <input type="text" name="truename" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <!-- <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button> -->
        <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
      </div>

      <table id="table-instance" lay-filter="table-instance"></table>

      <script type="text/html" id="table-useradmin-admin">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <!-- {{#  if(d.role == '超级管理员'){ }}
          <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
        {{#  } else { }} -->
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-4"></i>删除</a>

        <!-- {{#  } }}   -->
      </script>
    </div>
  </div>
</div>

<script>
  layui.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table'], function () {
    var $ = layui.$
      , admin = layui.admin
      , view = layui.view
      , setter = layui.setter
      , table = layui.table
      , form = layui.form;

    form.render(null, 'layadmin-useradmin-formlist');

    //监听搜索
    form.on('submit(LAY-user-back-search)', function (data) {
      var field = data.field;

      //执行重载
      table.reload('table-instance', {
        where: field
      });
    });
    //事件
    var active = {
      add: function () {
        admin.popup({
          title: '添加管理员'
          , area: ['420px', '450px']
          , id: 'LAY-popup-useradmin-add'
          , success: function (layero, index) {
            view(this.id).render('user/administrators/adminform').done(function () {
              form.render(null, 'layuiadmin-form-admin');
              //监听提交
              form.on('submit(LAY-user-back-submit)', function (data) {
                var field = data.field; //获取提交的字段
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                //$.ajax({});
                editUserHandle(Object.assign(data, field));
                layui.table.reload('LAY-user-front-submit'); //重载表格
                layer.close(index); //执行关闭 
              });
            });
          }
        });
      }
    };
    table.render({
      elem: "#table-instance",
      url: setter.request.baseUrl + "admin/list",
      method: "get",
      headers: { token: layui.data("layuiAdmin").token },
      response: {
        statusName: "code",
        statusCode: 0,
        msgName: "msg",
        countName: "totalCount",
        dataName: "admin",
      },
      cols: [
        [
          { field: "adminId", width: "10%", title: "ID", sort: true },
          { field: "username", title: "用户名", },
          { field: "truename", title: "真实姓名", },
          { field: "userpass", title: "密码", },
          { field: "role", title: "角色", templet: "#role" },
          { fixed: "right", title: "操作", toolbar: "#table-useradmin-admin", width: 150 },
        ],
      ],
      page: true,
    });
    //删除模块
    table.on("tool(table-instance)", function (obj) {
      var data = obj.data;
      if (obj.event === "del") {
        if (data.role == 0) {
          alert("不能删除超级管理员")
        } else {
          layer.confirm("真的删除么", function (index) {
            delAdminHandle({
              adminId: data.adminId,
            });
            obj.del();
            layer.close(index);
          });
        }
      } else if (obj.event === "edit") {
        admin.popup({
          title: "编辑用户",
          area: ["500px", "500px"],
          success: function (layero, index) {
            view(this.id)
              .render("user/administrators/adminform", data)
              .done(function () {
                let _data = data;
                form.render(null, "layuiadmin-form-admin");
                form.on('submit(LAY-user-back-submit)', function (data) {
                  var field = data.field;
                  // warning
                  editUserHandle(Object.assign(_data, field));

                  // layui.table.reload('LAY-user-back-manage'); //重载表格
                  layer.close(index);
                });
              });
          },
        });
      }
    });
    //修改编辑
    function editUserHandle(params) {
      admin.req({
        url: setter.request.baseUrl + "admin/save",
        data: JSON.stringify(params),
        type: "post",
        dataType: "json"
        , contentType: "application/json",
        done: function (res) {
          layui.table.reload("table-instance");
        },
      });
    }

    //删除
    function delAdminHandle(params) {
      admin.req({
        url: setter.request.baseUrl + "admin/delete",
        data: {
          ...params,
        },
        type: "post",
        done: function (res) {
          layer.msg(res.msg);
        },
      });
    }

    $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });


</script>

<script type="text/html" id="role">
  {{#  if(d.role == 0){ }}
  {{# return '超级管理员' }}
  {{#  } }}

  {{#  if(d.role == 1){ }}
    {{# return '奇点管理员' }}
  {{#  } }}
  {{#  if(d.role == 2){ }}
    {{# return '景区管理员' }}
  {{#  } }}


</script>